package cn.bob.web.controller;

import cn.bob.dto.BuyNumDto;
import cn.bob.dto.SelectByMovieDto;
import cn.bob.query.MovieTicketQuery;
import cn.bob.result.JsonResult;
import cn.bob.result.PageList;
import cn.bob.service.IMovieTicketService;
import cn.bob.domain.MovieTicket;

import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(movieTicketService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(movieTicketService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody MovieTicketQuery query){
        Page<MovieTicket> page = new Page<MovieTicket>(query.getPage(),query.getRows());
        page = movieTicketService.selectPage(page);
        return JsonResult.success(new PageList<MovieTicket>(page.getTotal(),page.getRecords()));
    }

    /**
     * 购买接口
     */
    @RequestMapping(value = "/buy",method = RequestMethod.POST)
    public JsonResult buy(@RequestBody @Valid BuyNumDto buyNumDto){
        movieTicketService.buy(buyNumDto);
        return JsonResult.success();
    }
    /**
     * 根据电影id查询接口
     */
    @RequestMapping(value = "/selectByMovie",method = RequestMethod.POST)
    public JsonResult selectByMovie(@RequestBody @Valid SelectByMovieDto selectByMovieDto){
        return movieTicketService.selectByMovie(selectByMovieDto);
    }

    /**
     * 获取随机电影票一张
     */
    @RequestMapping(value = "/randomMovie/{userId}",method = RequestMethod.POST)
        public JsonResult randomMovie(@PathVariable("userId")  Long userId){
        return movieTicketService.randomMovie(userId);
    }
    /**
     * 看电影
     */
    @RequestMapping(value = "/seeMovie",method = RequestMethod.POST)
        public JsonResult seeMovie(@RequestBody @Valid BuyNumDto buyNumDto){
        return movieTicketService.seeMovie(buyNumDto);
    }

}
